window.addEventListener('load', function() {
    // 1.获取元素
    var arrow_l = document.querySelector('.arrow-l');
    var arrow_r = document.querySelector('.arrow-r');
    var focus = document.querySelector('.focus');
    var ul = focus.querySelector('ul');
    var focusWidth = focus.offsetWidth;

    // 2.鼠标经过focus 显示左右按钮   
    focus.addEventListener('mouseenter', function() {
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
        // 清除定时器
        clearInterval(timer);
        timer = null;
    })
    focus.addEventListener('mouseleave', function() {
            arrow_l.style.display = 'none';
            arrow_r.style.display = 'none';
            // 开启定时器
            timer = setInterval(function() {
                arrow_r.click();
            }, 3000);
        })
        // 4.克隆第一张图片
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    // 3.右侧按钮点击事件
    var num = 0;
    // 6.节流阀
    var flag = true;
    arrow_r.addEventListener('click', function() {
            if (flag) {
                flag = false;
                if (num == ul.children.length - 1) {
                    ul.style.left = 0;
                    num = 0;
                }
                num++;
                animate(ul, -num * focusWidth, function() {
                    flag = true;
                });
            }
        })
        // 4.左侧按钮点击事件
    arrow_l.addEventListener('click', function() {
            if (flag) {
                flag = false;
                if (num == 0) {
                    num = ul.children.length - 1;
                    ul.style.left = -num * focusWidth + 'px';
                }
                num--;
                animate(ul, -num * focusWidth, function() {
                    flag = true;
                });

            }
        })
        // 5.定时器 自动播放
    var timer = setInterval(function() {
        arrow_r.click();
    }, 3000);
})